package com.net.common.web;

import java.util.List;

import com.net.common.util.MoneyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import freemarker.template.SimpleNumber;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

/**
 * @author  on 2017/7/18.
 * Money的格式化数据
 */
public class MoneyTemplateMethodModelEx implements TemplateMethodModelEx {
    private static final Logger LOGGER = LoggerFactory.getLogger(MoneyTemplateMethodModelEx.class);

    @Override
    public Object exec(List arguments) throws TemplateModelException {
        if (arguments == null || arguments.size() != 1) {
            LOGGER.warn("Wrong money arguments");
            return "";
        }
        SimpleNumber value = (SimpleNumber) arguments.get(0);

        if (value == null || value.getAsNumber() == null) {
            LOGGER.warn("Wrong money arguments empty");
            return "";
        }
        return MoneyUtil.convertCentToString(value.getAsNumber().longValue(), 2);
    }
}
